#include "ModbusSlave_Task.h"
#include "74HC595.h"
#include "Output.h"
#include "modbus.h"

void ModbusSlave_Task(void* pdata) {
    Sys_Base.para.slave_id = Get_Slaveid();   // 模拟量基板，默认1
    Sys_Base.para.type     = 0;               // 类型，默认0
    Sys_Base.version       = 0x01;            // 版本号
    for (;;) {
        WaitUartIdle(UART1_CON, 0);
        Modbus_action();
        OSTimeDly(100);
    }
}

static ModbusSlave_DataStructTypedef DataList[] = {
    {0x0000, ModbusSlave_R, &Sys_Base.para.slave_id, 1, NULL, NULL},         // 从机地址
    {0x0001, ModbusSlave_R, &Sys_Base.para.type, 1, NULL, NULL},             // 模块类型
    {0x0002, ModbusSlave_R, &Sys_Base.para.mod_num, 1, NULL, NULL},          // 扩展模块数
    {0x0003, ModbusSlave_RW, &Sys_Base.para.work_state, 1, NULL, NULL},      // 工作状态
    {0x0004, ModbusSlave_RW, &Sys_Base.ctrl_state, 1, Reset_System, NULL},   // 控制状态
    {0x0005, ModbusSlave_R, &Sys_Base.version, 1, NULL, NULL},               // 版本号

    {0x0064, ModbusSlave_R, &Input.din[0].cur_state, 1, NULL, NULL},   // 开关量输入状态
    {0x0065, ModbusSlave_R, &Input.din[1].cur_state, 1, NULL, NULL},
    {0x0066, ModbusSlave_R, &Input.din[2].cur_state, 1, NULL, NULL},
    {0x0067, ModbusSlave_R, &Input.din[3].cur_state, 1, NULL, NULL},
    {0x0068, ModbusSlave_R, &Input.din[4].cur_state, 1, NULL, NULL},
    {0x0069, ModbusSlave_R, &Input.din[5].cur_state, 1, NULL, NULL},
    {0x006A, ModbusSlave_R, &Input.din[6].cur_state, 1, NULL, NULL},
    {0x006B, ModbusSlave_R, &Input.din[7].cur_state, 1, NULL, NULL},

    {0x006C, ModbusSlave_RW, &Input.din[0].cur_count, 1, NULL, NULL},   // 当前脉冲总数
    {0x006D, ModbusSlave_RW, &Input.din[1].cur_count, 1, NULL, NULL},
    {0x006E, ModbusSlave_RW, &Input.din[2].cur_count, 1, NULL, NULL},
    {0x006F, ModbusSlave_RW, &Input.din[3].cur_count, 1, NULL, NULL},
    {0x0070, ModbusSlave_RW, &Input.din[4].cur_count, 1, NULL, NULL},
    {0x0071, ModbusSlave_RW, &Input.din[5].cur_count, 1, NULL, NULL},
    {0x0072, ModbusSlave_RW, &Input.din[6].cur_count, 1, NULL, NULL},
    {0x0073, ModbusSlave_RW, &Input.din[7].cur_count, 1, NULL, NULL},

    {0x0074, ModbusSlave_RW, &Input.din[0].cur_closetime, 1, NULL, NULL},   // 当前吸合时长
    {0x0075, ModbusSlave_RW, &Input.din[1].cur_closetime, 1, NULL, NULL},
    {0x0076, ModbusSlave_RW, &Input.din[2].cur_closetime, 1, NULL, NULL},
    {0x0077, ModbusSlave_RW, &Input.din[3].cur_closetime, 1, NULL, NULL},
    {0x0078, ModbusSlave_RW, &Input.din[4].cur_closetime, 1, NULL, NULL},
    {0x0079, ModbusSlave_RW, &Input.din[5].cur_closetime, 1, NULL, NULL},
    {0x007A, ModbusSlave_RW, &Input.din[6].cur_closetime, 1, NULL, NULL},
    {0x007B, ModbusSlave_RW, &Input.din[7].cur_closetime, 1, NULL, NULL},

    {0x0096, ModbusSlave_R, &Input.analog.pre, 1, NULL, NULL},           // 静压值
    {0x0097, ModbusSlave_R, &Input.analog.ad_value[0], 1, NULL, NULL},   // 模拟输入AD值
    {0x0098, ModbusSlave_R, &Input.analog.ad_value[1], 1, NULL, NULL},
    {0x0099, ModbusSlave_R, &Input.analog.ad_value[2], 1, NULL, NULL},
    {0x009A, ModbusSlave_R, &Input.analog.ad_value[3], 1, NULL, NULL},
    {0x009B, ModbusSlave_R, &Input.analog.ad_value[4], 1, NULL, NULL},
    {0x009C, ModbusSlave_R, &Input.analog.ad_value[5], 1, NULL, NULL},
    {0x009D, ModbusSlave_R, &Input.analog.ad_value[6], 1, NULL, NULL},
    {0x009E, ModbusSlave_R, &Input.analog.ad_value[7], 1, NULL, NULL},
    {0x009F, ModbusSlave_R, &Input.analog.ad_value[8], 1, NULL, NULL},
    {0x00A0, ModbusSlave_R, &Input.analog.ad_value[9], 1, NULL, NULL},
    {0x00A1, ModbusSlave_R, &Input.analog.ad_value[10], 1, NULL, NULL},
    {0x00A2, ModbusSlave_R, &Input.analog.ad_value[11], 1, NULL, NULL},
    {0x00A3, ModbusSlave_R, &Input.analog.ad_value[12], 1, NULL, NULL},
    {0x00A4, ModbusSlave_R, &Input.analog.ad_value[13], 1, NULL, NULL},
    {0x00A5, ModbusSlave_R, &Input.analog.ad_value[14], 1, NULL, NULL},
    {0x00A6, ModbusSlave_R, &Input.analog.ad_value[15], 1, NULL, NULL},
    {0x00A7, ModbusSlave_R, &Input.analog.ad_value[16], 1, NULL, NULL},
    {0x00A8, ModbusSlave_R, &Input.analog.ad_value[17], 1, NULL, NULL},
    {0x00A9, ModbusSlave_R, &Input.analog.ad_value[18], 1, NULL, NULL},
    {0x00AA, ModbusSlave_R, &Input.analog.ad_value[19], 1, NULL, NULL},
    {0x00AB, ModbusSlave_R, &Input.analog.ad_value[20], 1, NULL, NULL},
    {0x00AC, ModbusSlave_R, &Input.analog.ad_value[21], 1, NULL, NULL},

};
void Task_ModbusSlave_Init(void) {
    ModbusSlave_Init(DataList, (sizeof(DataList) / sizeof(DataList[0])));
}

uint8_t Reset_System(uint16_t addr, uint8_t cmd, uint16_t length, uint8_t* data, uint32_t para) {
    if (cmd != 0x03 && Sys_Base.ctrl_state == 1) {
        SystemReset();
    }
    return 1;
}
